<?php

Yii::import('admin.models._base.BaseCatalog');

class Catalog extends BaseCatalog
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

    public function img() {
        if( count($this->itemImages) > 0 ) {
            return $this->itemImages[0]->img_url;
        }
        else return "/images/no_img.png";
    }
	public function datarus(){

	}
	public function relations() {
		return array_merge(
			parent::relations(),
			array(
				'itemImages' => array(self::HAS_MANY, 'ItemImage', 'item_id','order'=>'`position` ASC'),
			)
		);
	}
	public function behaviors()
	{
		return array(
			'ml' => array(
				'class' => 'application.modules.admin.models.behaviors.MultilingualBehavior',
				'localizedAttributes' => array('title'),
				'langClassName' => 'CatalogLang',
				'langTableName' => 'catalog_lang',
				'languages' => Yii::app()->params['translatedLanguages'],
				'defaultLanguage' => Yii::app()->params['defaultLanguage'],
				'langForeignKey' => 'owner_id',
				'dynamicLangClass' => true,
			),
		);
	}

	public function defaultScope()
	{
		return $this->ml->localizedCriteria();
	}
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('t.t.id', $this->id);
		$criteria->compare('t.cid', $this->cid);
		$criteria->compare('t.isVisible', $this->isVisible, true);
		$criteria->compare('t.date', $this->date, true);
		$criteria->compare('t.author', $this->author);
		$criteria->compare('t.view', $this->view);
		$criteria->compare('t.count', $this->count);
		$criteria->compare('t.article', $this->article, true);
		$criteria->compare('t.id_1c', $this->id_1c);
		$criteria->compare('t.price', $this->price);
		$criteria->compare('t.old_price', $this->old_price);
		$criteria->compare('t.file_tech_doc', $this->file_tech_doc, true);
		$criteria->compare('t.file_eco_calc', $this->file_eco_calc, true);
		$criteria->compare('t.is_new', $this->is_new, true);
		$criteria->compare('t.brand_id', $this->brand_id);
		$criteria->compare('t.title', $this->title, true);
		$criteria->compare('t.small_description', $this->small_description, true);
		$criteria->compare('t.full_description', $this->full_description, true);
		$criteria->compare('t.technical_characteristics', $this->technical_characteristics, true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$this->ml->modifySearchCriteria($criteria),
			//instead of
			//'criteria'=>$criteria,
		));
	}
}